package com.bckj.fastboot.upms.biz.service;

import com.bckj.fastboot.core.lang.exception.BusinessException;
import com.bckj.fastboot.upms.api.constant.UpmsCacheConstant;
import com.bckj.fastboot.upms.biz.repository.SysParamRepository;
import com.bckj.fastboot.upms.biz.convert.SysParamConvert;
import com.bckj.fastboot.upms.api.vo.SysParamVO;
import com.bckj.fastboot.upms.api.dto.SysParamRequest;
import com.bckj.fastboot.upms.api.dto.SysParamQuery;
import com.bckj.fastboot.upms.api.entity.SysParam;

import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统参数表(SysParam)domain-service
 *
 * @author wfc
 * @since 2023-09-09 10:50:46
 */
@Service
@RequiredArgsConstructor
public class SysParamService {

    private final SysParamRepository sysParamRepository;
	
    private final SysParamConvert sysParamConvert;
	
    /**
     * 列表
     *
     * @param query req
     * @return list
     */
    public List<SysParamVO> list(SysParamQuery query){
        return sysParamRepository.getBaseMapper().list(query);
    }

    /**
     * 保存
     *
     * @param request req
     */
    public boolean save(SysParamRequest request) {
        return sysParamRepository.save(sysParamConvert.request2Bean(request));
    }

    /**
     * 修改
     *
     * @param request req
     */
    public boolean update(SysParamRequest request) {
        return sysParamRepository.updateById(sysParamConvert.request2Bean(request));
    }
    
    /**
     * 详情
     *
     * @param id id
     * @return vo
     */
    public SysParamVO details(Long id) {
        SysParam bean = sysParamRepository.getById(id);
        if (null == bean) {
            throw new BusinessException("当前对象不存在!");
        }
        return sysParamConvert.bean2VO(bean);
    }

	/**
	 * 根据Id移除
	 *
	 * @param id id
	 * @return boolean
	 */
	public boolean remove(Long id) {
        //TODO LIMIT
		return sysParamRepository.removeById(id);
	}

    @Cacheable(value = UpmsCacheConstant.DICT_TYPE, key = "a0")
    public SysParam getByName(String name) {
        return sysParamRepository.lambdaQuery().eq(SysParam::getName, name).eq(SysParam::getDeleteFlag, 0).one();
    }
}

